package com.mysql.opt.indexmerge.controller;

import com.mysql.opt.indexmerge.entity.OrderDetail;
import com.mysql.opt.indexmerge.entity.OrderInfo;
import com.mysql.opt.indexmerge.service.OrderDetailService;
import com.mysql.opt.indexmerge.service.OrderInfoService;
import com.mysql.opt.indexmerge.vo.OrderInfoVo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
public class OrderInfoController implements CommandLineRunner {

    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private OrderDetailService detailService;

    @Override
    public void run(String... args) throws Exception {

        Long beginId = 0L;
        boolean hasData = true;
        do {
            OrderInfoVo orderInfo = new OrderInfoVo();
            orderInfo.setId(beginId);
            List<OrderInfo> orderInfos = orderInfoService.queryList(orderInfo);
            if (orderInfos != null && orderInfos.size() > 0) {
                beginId = orderInfos.get(orderInfos.size() - 1).getId();

                List<OrderDetail> details = new ArrayList<>(orderInfos.size());

                for (OrderInfo info : orderInfos) {
                    OrderDetail detail = new OrderDetail();
                    detail.setOrderId(info.getId());
                    detail.setBusinessName(generateName());
                    detail.setBusinessAddress(generateName());
                    detail.setAddress(generateName());
                    details.add(detail);
                }
                detailService.saveBatch(details);

                System.out.println("批量插入完成...");
            } else {
                hasData = false;
            }
        } while (hasData);


    }

    public String generateName() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 17);
    }

}